//
//  UIView+ProgressHUD.m
//  TzyjMainProj
//
//  Created by DF_ZK on 3/16/17.
//  Copyright © 2017 orientsec. All rights reserved.
//

#import "UIView+ProgressHUD.h"

#define DEFAULTTIMEOUT  30

#define SHOWTTIMEOUTVIEWTIME  2

@interface UIView ()

@end

@implementation UIView (ProgressHUD)

+ (void)showHUD:(NSString *)text
{
    UIView *window = [[UIApplication sharedApplication] keyWindow];
    
    [MBProgressHUD hideHUDForView:window animated:NO];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:window animated:YES];
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.bezelView.backgroundColor = [UIColor blackColor];
    hud.label.text = text;
    hud.contentColor = [UIColor whiteColor];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(DEFAULTTIMEOUT * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self hudTimeout:hud completed:nil];
    });
}

+ (void)showHUD:(NSString *)text timeout:(NSInteger)timeout
{
    UIView *window = [[UIApplication sharedApplication] keyWindow];
    
    [MBProgressHUD hideHUDForView:window animated:NO];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:window animated:YES];
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.bezelView.backgroundColor = [UIColor blackColor];
    hud.label.text = text;
    hud.contentColor = [UIColor whiteColor];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(timeout * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self hudTimeout:hud completed:nil];
    });
}

+ (void)showHUD:(NSString *)text completed:(void (^)())completed
{
    UIView *window = [[UIApplication sharedApplication] keyWindow];
    
    [MBProgressHUD hideHUDForView:window animated:NO];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:window animated:YES];
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.bezelView.backgroundColor = [UIColor blackColor];
    hud.label.text = text;
    hud.contentColor = [UIColor whiteColor];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(DEFAULTTIMEOUT * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self hudTimeout:hud completed:completed];
    });
}

+ (void)showHUD:(NSString *)text timeout:(NSInteger)timeout completed:(void (^)())completed
{
    UIView *window = [[UIApplication sharedApplication] keyWindow];
    
    [MBProgressHUD hideHUDForView:window animated:NO];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:window animated:YES];
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.bezelView.backgroundColor = [UIColor blackColor];
    hud.label.text = text;
    hud.contentColor = [UIColor whiteColor];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(timeout * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self hudTimeout:hud completed:completed];
    });
}

+ (void)hideHUD
{
    UIView *window = [[UIApplication sharedApplication] keyWindow];
    
    [MBProgressHUD hideHUDForView:window animated:YES];
}

+ (void)hudTimeout:(MBProgressHUD *)mbHud completed:(void (^)())completed
{
    UIImageView *view = [[UIImageView alloc] initWithImage:[UIImage loadImageNoCache:@"hud_timeout_face.png"]];
    
    mbHud.mode = MBProgressHUDModeCustomView;
    mbHud.customView = view;
    mbHud.label.text = @"请求超时";
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(SHOWTTIMEOUTVIEWTIME * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [mbHud hideAnimated:YES];
        if (completed) {
            completed();
        }
    });
}

@end
